/**

 * @author admin

 * 2014年7月5日

 */

package com.tmall.testgen.plugin.views.vo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.tmall.testgen.common.util.Object2TreeMap;
import com.tmall.testgen.common.util.StringUtil;
import com.tmall.testgen.common.util.bo.FieldClassAndValue;

public class TreeParent extends TreeObject {
	
	
	/**
	 * 是否叶子节点
	 */

	private boolean isLeafNode = false;
	


	private List<TreeObject> children;
	
	/**
	 * 当前节点所代表的值
	 */
	protected Object value;
	
	/**
	 * 是否已经处理过
	 */
	protected boolean isHandled;



	public boolean isLeafNode() {
		return isLeafNode;
	}

	public void setLeafNode(boolean isLeafNode) {
		this.isLeafNode = isLeafNode;
	}

	public TreeParent() {
		super("");
		children = new ArrayList<TreeObject>();
	}

	public TreeParent(String name) {
		super(name);
		children = new ArrayList<TreeObject>();
	}

	public void addFieldValue(){
		if(isHandled)
			return;
		
		isHandled=true;
		//Object temValue=value;
//		if(value instanceof ){
//			
//		}
		
		Map<String, FieldClassAndValue>  fieldValues=Object2TreeMap.getMapFromObject(value,false);
		if(fieldValues==null)
			return ;
		
		this.setLeafNode(false);
		
		for(String name:fieldValues.keySet()){
			FieldClassAndValue  fv=fieldValues.get(name);
			TreeParent node = new TreeParent(name+":    "+StringUtil.getTreeNodeName(fv.getValue()));
			node.setValue(fv.getValue());
			node.setLeafNode(true);
			this.addChild(node);
		}
		
	}
	public void addChild(TreeObject child) {
			child.setEditable(super.isEditable());

			children.add(child);
			child.setParent(this);

	}

	public void removeAllChildren(){
		children=new ArrayList<TreeObject>();;
	}
	public void removeChild(TreeObject child) {
		children.remove(child);
		child.setParent(null);
	}

	public TreeObject[] getChildren() {
		return (TreeObject[]) children.toArray(new TreeObject[children
				.size()]);
	}

	public boolean hasChildren() {
		if (isLeafNode) {
			return false;
		} else {
			return children.size() > 0;
		}
	}

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}
	
	
	
}
